package cn.edu.ctbu.bootstraptablepage.service;

import cn.edu.ctbu.bootstraptablepage.domain.Student;
import cn.edu.ctbu.bootstraptablepage.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentrepository;

    /**
     * 获取所有学生的信息
     * @param pageable 未
     * @return studentrepository.findAll(pageable)
     */
    public Page<Student> getAll(Pageable pageable) {
        return studentrepository.findAll(pageable);
    }

    public List<Student> getAll(){
        return studentrepository.findAll();
    }
    /***
     *  插入
     *  插入职工信息
     * @return
     */
    public Student insert(Student student) {
        studentrepository.save(student);
        return student;
    }

    /***
     *  更新
     *  更新职工信息
     * @return
     */
    public Student update(Student student) {

        studentrepository.save(student);
        return student;
    }

    /***
     *  删除
     *  删除职工信息
     */
    public void delete(Student student) {
        studentrepository.delete(student);
    }

    public void save(Student student) {
        studentrepository.save(student);
    }

    public List<Student> findByNameAndPassword(String name, String password) {
        return studentrepository.findByNameAndPassword(name, password);
    }
    /**
     * 按名字进行查询
     * @param pageable 未
     * @param name 学生姓名
     * @return studentrepository.findByName(name, pageable)
     */
    public Page<Student> findByName(String name, Pageable pageable) {
        return studentrepository.findByName(name, pageable);
    }

    /**
     * 按学号进行查询
     * @param pageable 未
     * @param no 学生学号
     * @return studentrepository.findByNo(no, pageable)
     */
    public Page<Student> findByNo(String no, Pageable pageable) {
        return studentrepository.findByNo(no, pageable);
    }

    /**
     * 按id进行查询
     * @param pageable 未
     * @param id 学生id
     * @return studentrepository.findById(id, pageable);
     */
    public Page<Student> findById(Integer id, Pageable pageable) {
        return studentrepository.findById(id, pageable);
    }


    /**
     * 按id进行查询 - updata
     * @param id 学生id
     * @return studentrepository.findById(id).orElse( null)
     */
    public Student findById(Integer id) {
        return studentrepository.findById(id).orElse( null);
    }

}
